/*
 * conexiones.h
 *
 *  Created on: 22/11/2011
 *      Author: utn_so
 */

#ifndef CONEXIONES_H_
#define CONEXIONES_H_

#define READ 	'1'
#define WRITE 	'2'
#define VECTORCOMIENZAENCERO 1
#define PISARULTIMOSECTORINCOMPLETO 1


#include <time.h>
#include "nipc.h"

typedef enum {
	FREE = 1,
	BUSY = 2,
}status;

typedef struct {
		int		socket;
		status 	libre;
}t_conexion;

typedef enum
{
	NOMODIFICADO = 0,
	MODIFICADO = 1,
}bitMod;

typedef struct{

	int 	sector;
	char 	buffer[512]; //512 bytes
	time_t 	*timeStand;
	bitMod 	modificado;
	int 	sectoresEnCache;
}t_cache;

typedef struct{

	char *path;
	t_cache *cache;
	pthread_mutex_t semCache;

}TDD_entry;

typedef struct
{
	unsigned int 	sector;
	char			buffer[512];
}sec_arch;

t_conexion available_connection();

int connect_Raid_PPD();

void armarPayload(nipcMessage msg, unsigned int sector, char *buffer);

char *check_cache(t_cache *cache, int tamanio, int sector, int pos);

int cargarCache(char buffer[512], int sector, int sectoresEnCache, t_cache *cache, bitMod bitDeModificado);

int crearCache(t_cache *cache);

void armarBuf(sec_arch *sectores, int tamanio, char *buf, int w, int w2, unsigned int bloque, int secPerBloque);

int buscarArchEnTDD(const char *path);

int leerSector(unsigned int sector, int posicionEnTDD, int sectoresEnCache, char *buffer);

int posicionEnTDD(const char *path);

unsigned int *crearListaDeSectores(unsigned int primerSector, int cantidad);

void escribirEnDisco(char buff[512], unsigned int sector);

void escribirBufEnCache(const char *path, const char *buf, int numSector, unsigned int *listaSectoresClusterLibre);

void guardarInfoEnClusters(int sectoresEnBuf, const char *path, const char *buf, int tamanio);

void guardarInfoSectorLleno(int posicionS, int sectoresEnBuf, const char *path, const char *buf, int tamanio, unsigned int *listaSectoresClusterN);

char *getSector(unsigned int sector);

void setSector(unsigned int sector, char *contenido);

void getBloque(unsigned int sector, int bloque, int secPerBloque, sec_arch *sectores, int tamanio, int posicion, int sectoresEnCache);

void setBloque(unsigned int sector, int bloque,int secPerBloque, sec_arch *sectores, int tamanio, int posicion, int sectoresEnCache);

#endif /* CONEXIONES_H_ */
